<template>
  <div class="html">
    <div id="allmap"></div>
    <div class="header">
      <router-link class="city" to="/city">{{ city }}</router-link>
      <router-link to="/search">
        <van-field
          class="field"
          v-model="value1"
          left-icon="search"
          placeholder="搜索商品"
        />
      </router-link>
    </div>
    <van-swipe class="my-swipe" :autoplay="3000" indicator-color="white">
      <van-swipe-item class="img" v-for="item in list" :key="item.id">
        <img :src="item.image_url" alt="" />
      </van-swipe-item>
    </van-swipe>
    <div class="grid">
      <div
        class="grid-item"
        v-for="item in grid"
        :key="item.id"
        @click="toMain('/main', item.id)"
      >
        <img :src="item.icon_url" alt="" />
        <p class="title">{{ item.name }}</p>
      </div>
    </div>
    <div class="brandList-title">品牌制造商直供</div>
    <div class="brandList-img">
      <div
        class="box"
        v-for="item in brandList"
        :key="item.id"
        @click="toMain('/brand', item.id)"
      >
        <div class="box-div">
          <p>{{ item.name }}</p>
          <p>{{ item.floor_price }}元起</p>
        </div>
        <img :src="item.new_pic_url" alt="" />
      </div>
    </div>
    <div class="newgoods-top">
      <div class="top"  @click="toGoodsList('isNew',1)">
        <p class="p1">新品首发</p>
        <p class="p2">点击查看</p>
      </div>
    </div>
    <ul class="newGoods">
      <li
        class="newGoods-box"
        @click="toMain('/detail', item.id)"
        v-for="item in newGoods"
        :key="item.id"
      >
        <img :src="item.list_pic_url" alt="" />
        <div class="newGoods-text">
          <p>{{ item.name }}</p>
          <p>{{ item.goods_brief }}</p>
          <p>{{ item.retail_price }}元起</p>
        </div>
      </li>
    </ul>
    <div class="newgoods-bottom">
      <div class="top"  @click="toGoodsList('isNew',1)">
        <p class="p1">人气推荐.好物精选</p>
        <p class="p2">点击查看</p>
      </div>
    </div>
    <ul class="hotGoods">
      <li
        class="hotGoods-box"
        @click="toMain('/detail', item.id)"
        v-for="item in hotGoods"
        :key="item.id"
      >
        <img :src="item.list_pic_url" alt="" />
        <div class="hotGoods-text">
          <p>{{ item.name }}</p>
          <p>{{ item.goods_brief }}</p>
          <p>{{ item.retail_price }}元起</p>
        </div>
      </li>
    </ul>
    <div class="topicList-title">专题精选</div>
    <ul class="topicList">
      <li class="topicList-box" v-for="item in topicList" :key="item.id">
        <img :src="item.item_pic_url" alt="" />
        <div class="topicList-text">
          <p class="p1">
            <span>{{ item.title }}</span
            ><span>{{ item.price_info }}元起</span>
          </p>
          <p>{{ item.subtitle }}</p>
        </div>
      </li>
    </ul>
    <div
      class="newCategoryList"
      v-for="(item, index) in newCategoryList"
      :key="index"
    >
      <div class="goodsList-title">{{ item.name }}好物</div>
      <div class="goodsList">
        <router-link
          :to="{ path: '/detail', query: { id: time.id } }"
          class="goodsList-box"
          v-for="time in newCategoryList[index].goodsList"
          :key="time.id"
        >
          <img :src="time.list_pic_url" alt="" />
          <div class="goodsList-text">
            <p>{{ time.name }}</p>
            <p>￥{{ time.retail_price }}</p>
          </div>
        </router-link>
        <div class="goodsList-box">
          <p class="goodsList-box-p">{{ item.name }}好物</p>
          <img class="img" src="../../../../images/icon_go_more.png" alt="" />
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { index, goodsList } from "@/api/home";
// import axios from "axios";
export default {
  data() {
    return {
      list: [],
      grid: [],
      brandList: [],
      hotGoods: [],
      newGoods: [],
      topicList: [],
      newCategoryList: [],
      value1: "",
      city: "",
    };
  },
  created() {
    index().then((res) => {
      this.list.push(...res.banner);
      this.grid.push(...res.channel);
      this.brandList.push(...res.brandList);
      this.newGoods.push(...res.newGoods);
      this.hotGoods.push(...res.hotGoods);
      this.topicList.push(...res.topicList);
      this.newCategoryList = res.newCategoryList;
      console.log(res);
    });
    goodsList().then((res) => {
      console.log(res);
    });
  },
  mounted() {
    let address = localStorage.getItem("address");
    if (address) {
      this.city = address;
    } else {
      var _this = this;
      var map = new BMap.Map("allmap");
      var point = new BMap.Point(116.331398, 39.897445);
      map.centerAndZoom(point, 12);

      var geolocation = new BMap.Geolocation();
      geolocation.getCurrentPosition(
        function (r) {
          localStorage.setItem("point", JSON.stringify(r.point));
          var result = r.address.city;
          _this.city = result;
        },
        { enableHighAccuracy: true }
      );
    }
  },
  methods: {
    toMain(to, id) {
      this.$router.push({ path: to, query: { id: id } });
    },
    toGoodsList(type, page) {
      if (type == "isNew") {
        this.$router.push({ path: "/newgoods", query: { isNew: page } });
      } else {
        this.$router.push({ path: "/newgoods", query: { isHot: page } });
      }
    },
  },
};
</script>

<style lang="scss" scoped>
* {
  margin: 0;
  padding: 0;
}
.html {
  background-color: #f4f4f4;
  margin-bottom: 50px;
}
.header {
  display: flex;
  // justify-content: space-between;
  padding: 0 10px;
  background-color: #fff;
}
.city {
  width: 50px;
  line-height: 44px;
  text-align: center;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.header .field {
  margin: auto 5px;
  padding: 10px;
}
.my-swipe {
  width: 100%;
  //   height: 200px;
}
.img {
  width: 100%;
  //   height: 200px;
}
.img img {
  width: 100%;
  //   height: 200px;
}
.grid {
  padding: 10px 0;
  background-color: #fff;
  display: flex;
  justify-content: space-around;
  margin-bottom: 20px;
  .grid-item {
    text-align: center;
    font-weight: 400;
    img {
      width: 30px;
      height: 30px;
    }
  }
}
.brandList-title {
  line-height: 40px;
  background-color: #fff;
}
.brandList-img {
  display: flex;
  justify-content: space-around;
  flex-wrap: wrap;
  margin: 0 5px;
}
.box {
  position: relative;
  width: 48%;
  background-color: #f4f4f4;
}
.box img {
  width: 100%;
}
.box .box-div {
  position: absolute;
  top: 5px;
  left: 5px;
  text-align: left;
  color: #000;
}
.box .box-div p {
  margin: 12px 0;
}
.newgoods-top {
  width: 100%;
  height: 130px;
  text-align: center;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: #eef5fc;
}
.newgoods-top .top p {
  margin-top: 16px;
}
.newgoods-top .p1 {
  color: #8c9bae;
  font-size: 18px;
}
.newgoods-top .p2 {
  width: 90px;
  height: 25px;
  background-color: #d8e4f0;
  line-height: 25px;
}
.newgoods-bottom {
  width: 100%;
  height: 130px;
  text-align: center;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: #fcf4de;
  color: #b1a279;
}
.newgoods-bottom .top p {
  margin-top: 16px;
}
.newgoods-bottom .p1 {
  font-size: 18px;
}
.newgoods-bottom .p2 {
  width: 90px;
  height: 25px;
  background-color: #f4e9cb;
  line-height: 25px;
  margin: 0 auto;
}
.newGoods {
  width: 375px;
  display: flex;
  // justify-content: flex-start;
  overflow: scroll;
  background-color: #fff;
}
.newGoods-box {
  width: 150px;
  margin: 0 10px;
  display: inline-block;
}
.newGoods-box img {
  width: 150px;
}
.newGoods-text {
  margin-left: 20px;
  width: 120px;
  text-align: left;
}
.newGoods-text p {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  margin: 4px 0 12px 0;
}
.newGoods-text p:nth-child(1) {
  font-size: 18px;
  font-weight: 700;
  color: #000;
}
.newGoods-text p:nth-child(2) {
  font-size: 12px;
  color: #8a8a8a;
}
.newGoods-text p:nth-child(3) {
  color: #9c3232;
}
.newGoods {
  width: 375px;
  display: flex;
  // justify-content: flex-start;
  overflow: scroll;
}
.newGoods-box {
  width: 150px;
  margin: 0 10px;
  display: inline-block;
}
.newGoods-box img {
  width: 150px;
}
.newGoods-text {
  margin-left: 20px;
  width: 120px;
  text-align: left;
}
.newGoods-text p {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  margin: 4px 0 12px 0;
}
.newGoods-text p:nth-child(1) {
  font-size: 18px;
  font-weight: 700;
  color: #000;
}
.newGoods-text p:nth-child(2) {
  font-size: 12px;
  color: #8a8a8a;
}
.newGoods-text p:nth-child(3) {
  color: #9c3232;
}

.hotGoods {
  width: 375px;
  display: flex;
  // justify-content: flex-start;
  overflow: scroll;
  margin-bottom: 20px;
  background-color: #fff;
}
.hotGoods-box {
  width: 150px;
  margin: 0 10px;
  display: inline-block;
}
.hotGoods-box img {
  width: 150px;
}
.hotGoods-text {
  margin-left: 20px;
  width: 120px;
  text-align: left;
}
.hotGoods-text p {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  margin: 4px 0 12px 0;
}
.hotGoods-text p:nth-child(1) {
  font-size: 18px;
  font-weight: 700;
  color: #000;
}
.hotGoods-text p:nth-child(2) {
  font-size: 12px;
  color: #8a8a8a;
}
.hotGoods-text p:nth-child(3) {
  color: #9c3232;
}

.topicList {
  width: 375px;
  display: flex;
  overflow: scroll;
  margin-bottom: 20px;
  background-color: #fff;
}
.topicList-title {
  margin: 0 auto;
  padding: 20px;
  background-color: #fff;
}
.topicList-box {
  width: 320px;
  margin: 0 10px;
  // display: inline-block;
}
.topicList-box img {
  width: 320px;
  height: 176px;
}
.topicList-text {
  // margin-left: 20px;
  // width: 300px;
  text-align: left;
}
.topicList-text p {
  margin: 4px 0 12px 0;
}
.topicList-text .p1 {
  font-size: 18px;
  font-weight: 700;
  color: #000;
}
.topicList-text .p1 span:nth-child(2) {
  color: #f00;
  float: right;
  font-size: 14px;
  font-weight: 500;
}
.topicList-text p:nth-child(2) {
  font-size: 12px;
  width: 260px;
  color: #8a8a8a;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.goodsList-title {
  margin: 0 auto;
  padding: 20px;
  // background-color: #fff;
}
.goodsList {
  width: 375px;
  display: flex;
  justify-content: space-evenly;
  flex-wrap: wrap;
}
.goodsList .goodsList-box {
  width: 47%;
  background-color: #fff;
  margin-bottom: 6px;
}
.goodsList .goodsList-box .goodsList-text {
  text-align: left;
}
.goodsList-text p {
  margin: 10px;
  color: #000;
  width: 160px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.goodsList-text p:nth-child(2) {
  color: #9c3232;
}
.goodsList .goodsList-box img {
  width: 100%;
}
.goodsList .goodsList-box .goodsList-box-p {
  margin-top: 50px;
  font-size: 18px;
  margin-bottom: 30px;
}
.goodsList .goodsList-box .img {
  width: 39px;
  height: 39px;
}
</style>